<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/20
 * Time: 10:50
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
class Sendname extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($keyword) && $keyword != ''){
                $where['s_name'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('send_status')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('send_status')
                ->where($where)
                ->order('s_sort asc')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $s_name = trim(input('s_name'), ' ');
            $s_hours = input('s_hours');
            $s_sort = input('s_sort');
            $data = [
                's_name' => $s_name,
                's_hours' => $s_hours,
                's_sort' => $s_sort,
                's_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_send_status')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function update(){
        if(Request::instance()->isPost()){
            $s_id = input('s_id');
            $s_name = trim(input('s_name'), ' ');
            $s_hours = input('s_hours');
            $s_sort = input('s_sort');
            $data = [
                's_name' => $s_name,
                's_hours' => $s_hours,
                's_sort' => $s_sort,
                's_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_send_status')->where('s_id', $s_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $s_id = input('s_id');
        // banner
        $banner = Db::table('up_send_status')
            ->where('s_id', $s_id)
            ->find();
        $this->assign([
            's_id' => $s_id,
            'data' => $banner
        ]);
        return $this->fetch();
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $banner_id = input('id');
        $res = Db::table('up_send_status')->where('s_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
    /**
     * 排序
     * @return \think\response\Json
     */
    public function editsort(){
        $id = input('id');
        $field = input('field');
        $value = input('value');
        $param = [
            $field => $value,
        ];
        $res = Db::name('send_status')->where('s_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
}